package com.xw.mailservice.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类
 * @author YangJianquan
 */
public class DateUtil {
	/**
	 * 获取今天的日期字符串，如20161011
	 * @return
	 */
	public static String getTodayStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(new Date());
	}
	
	public static String getTimeStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		return sdf.format(new Date());
	}
	
	public static String getDateTimeStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		return sdf.format(new Date());
	}
	
	
	
	public static String getDateTimeStr(Date date){
		if (date==null) {
			date = new Date();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		return sdf.format(date);
	}
	
	
	public static void main(String[] args) {
		System.out.println(getTimeStr());
	}

	public static long defTime(Date now, Date old) {
		long lnow = now.getTime();
		long lold = old.getTime();
		return lnow - lold;
	}
	
	/**
	 * 描述：字符串转日期
	 * @author 杨建全
	 * @date 2017年6月8日
	 * @return
	 * @throws ParseException 
	 */
	public static Date toDate(String timeStr) throws ParseException {
		DateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
		return f.parse(timeStr);
	}
}
